package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.plus.web.controller.BasePlusController;
import com.ruoyi.common.core.plus.web.page.PageDto;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.domain.SysRoleUnique;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.params.SysRoleParams;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 角色信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BasePlusController {
    @Autowired
    private ISysRoleService roleService;

    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysRoleParams role) {
//        startPage();
        PageDto pageDto = startPage();
        Page<SysRole> page = new Page(pageDto.getCurrentPage(), pageDto.getPageSize());
        Page<SysRole> list = roleService.selectRoleList(page,role);
        return getDataTable(list);
    }

    @Log(title = "角色管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:role:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRoleParams role) throws IOException {
        Page<SysRole> page = new Page<>(1, -1);
        Page<SysRole> list = roleService.selectRoleList(page,role);
        ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
        util.exportExcel(response, list.getRecords(), "角色数据");
    }

    /**
     * 根据角色编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @GetMapping(value = "/{deptId}/{roleKey}")
    public AjaxResult getInfo(@PathVariable Long deptId, @PathVariable String roleKey) {
        return AjaxResult.success(roleService.selectRole(deptId, roleKey));
    }
/*    @GetMapping(value = "/{roleId}")
    public AjaxResult getInfo(@PathVariable Long roleId)
    {
        return AjaxResult.success(roleService.selectRoleById(roleId));
    }*/

    /**
     * 新增角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:add')")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody SysRole role) {

        role.setCreateBy(SecurityUtils.getUsername());
        return R.ok(roleService.insertRole(role));

    }

    /**
     * 修改保存角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody SysRole role) {
        roleService.checkRoleAllowed(role);

        role.setUpdateBy(SecurityUtils.getUsername());
        return R.ok(roleService.updateRole(role));
    }

    /**
     * 修改保存数据权限
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/dataScope")
    public R dataScope(@RequestBody SysRole role) {
//        roleService.checkRoleAllowed(role);
        return R.ok(roleService.authDataScope(role));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R changeStatus(@RequestBody SysRole role) {
//        roleService.checkRoleAllowed(role);
        role.setUpdateBy(SecurityUtils.getUsername());
        return R.ok(roleService.updateRoleStatus(role));
    }

    /**
     * 删除角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:remove')")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping()
    public R remove(@RequestBody List<SysRoleUnique> sysRoleKeys) {
        return R.ok(roleService.deleteRoleByKeys(sysRoleKeys));
    }

    /**
     * 获取角色选择框列表
     */
    @GetMapping("/option-select/{deptId}")
    public AjaxResult optionSelect(@PathVariable Long deptId) {
        Page<SysRole> page = new Page<>(1, -1);
        SysRoleParams sysRoleParams = new SysRoleParams();
        sysRoleParams.setDeptId(deptId);
        return AjaxResult.success(roleService.selectRoleList(page,sysRoleParams));
    }
}